package com.taropan.wallpaper.ui;

import com.taropan.wallpaper.CrashApplication;
import com.taropan.wallpaper.R;
import com.taropan.wallpaper.util.ActivityManager;
import com.taropan.wallpaper.util.AppContext;
import com.taropan.wallpaper.util.BasicUIEvent;
import com.taropan.wallpaper.util.CommandTaskEvent;
import com.taropan.wallpaper.util.TextUtil;
import com.taropan.wallpaper.util.Utils;
import com.taropan.wallpaper.widget.CustomDialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BaseActivity extends Activity implements BasicUIEvent {

	private static final String TAG = BaseActivity.class.getSimpleName();
	protected Button btn_leftTop, btn_rightTop;
	protected TextView tv_head;
	public int CanvasHeight;
	public int CanvasWidth;
	public LinearLayout topHeight;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		CrashApplication application = (CrashApplication) getApplication();
		AppContext.setContext(this);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		TextUtil.setCtxForGetResString(this);
		ActivityManager.addActivity(this);
		DisplayMetrics displayMetrics = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		application.CanvasWidth = CanvasWidth = displayMetrics.widthPixels;
		application.CanvasHeight = CanvasHeight = displayMetrics.heightPixels;
		
	}

	protected void initHead() {
		btn_leftTop = (Button) findViewById(R.id.btn_leftTop);
		btn_rightTop = (Button) findViewById(R.id.btn_rightTop);
		tv_head = (TextView) findViewById(R.id.tv_head);
		topHeight = (LinearLayout)findViewById(R.id.toplayout);
		btn_rightTop.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(BaseActivity.this,SettingsActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		if (keyCode == KeyEvent.KEYCODE_BACK) {

			AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
			builder.setMessage("你确定退出吗？")
					.setCancelable(false)
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									finish();
									System.exit(0);
								}
							})
					.setNegativeButton("返回",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});
			AlertDialog alert = builder.create();
			alert.show();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}
	
	public void execute(int mes, Object obj) {

	}

	public void eventTask(int CommandID, String tips, Object obj) {
		try {
			CommandTaskEvent com = new CommandTaskEvent(this, this, "请稍候...");
			com.execute((new Object[] { CommandID + "", obj }));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void destroyDialogByFinish() {
		Message localMessage = Message.obtain(basicHandler, destroyDialogByFinish);
		basicHandler.sendMessage(localMessage);
	}

	public void destroyDialog() {
		basicHandler.sendEmptyMessage(DESTROYDIALOG);
	}
	public Handler basicHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case DESTROYDIALOG:
				if (dialog != null && dialog.isShowing()) {
					dialog.cancel();
				}
				break;
			case cancelAlertDialog:
				if (alert != null && alert.isShowing()) {
					alert.cancel();
				}
				break;
			case destroyDialogByFinish:
				if (dialog != null && dialog.isShowing()) {
					dialog.cancel();
				}
				BaseActivity.this.finish();
				break;
			default:
				break;
			}
		}
	};
	/************************************************ 系统信息 ***************************************************************/
	public static final int DESTROYDIALOG = 0x00000101;
	private final static int cancelAlertDialog = 0x00000102;
	private final static int destroyDialogByFinish = 0x00000103;
	public CustomDialog dialog;
	public AlertDialog alert = null;
	public boolean alertIsShow = false;
	public boolean prohibitBreak = false;

	public void loading(String msg) {
		try {
			if (this.isFinishing()) {
				return;
			}
			if (dialog == null || !dialog.isShowing()) {
				View progressview = LayoutInflater.from(BaseActivity.this)
						.inflate(R.layout.progress_dialog, null);
				CustomDialog.Builder progressBuilder = new CustomDialog.Builder(
						BaseActivity.this);
				progressBuilder.setContentView(progressview);
				dialog = progressBuilder.create();
				dialog.show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void finishAll() {
		try {
			if (dialog != null && dialog.isShowing()) {
				destroyDialogByFinish();
			} else {
				super.finish();
			}
			System.gc();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
